package com.azt.provider.impl;

import com.azt.api.pojo.Advertise;
import com.azt.api.pojo.AdvertiseImg;
import com.azt.api.service.AdvertiseService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.AdvertiseImgMapper;
import com.azt.provider.mapper.AdvertiseMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdvertiseServiceImpl implements AdvertiseService {
	@Autowired
	private AdvertiseMapper mapper;
	
	@Autowired
	private AdvertiseImgMapper imgMapper;
    
    @Override
    public Pagination getAdvertisePage(int pageno, int pagesize, Map<String, String> searchMap)
    {
        Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<Advertise> List = mapper.getAdvertiseByParams(searchMap);
		Pagination p = new Pagination(startPage, List);
		p.setList(List);
		p.setParams(searchMap);
        return p;
        
    }
    
    @Override
    public void saveAdvertise(Advertise advertise)
    {
        mapper.insertSelective(advertise);
    }
    
    @Override
    public void deleteAllById(Integer id)
    {
    	mapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public void saveEdit(Advertise advertise)
    {
    	mapper.updateByPrimaryKeySelective(advertise);
    }
    
	@Override
	public Advertise queryById(Integer id) 
	{
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public AdvertiseImg getAdImgById(int id) {
        return imgMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAdImg(AdvertiseImg advertiseImg) {
        imgMapper.updateByPrimaryKeySelective(advertiseImg);
    }

    @Override
    public Advertise getAdvertiseByCode(String code) {
        return mapper.getAdvertiseByCode(code);
    }

    @Override
    public List<AdvertiseImg> getAdvertiseImgsByPid(int id) {
        return imgMapper.getAdvertiseImgsByPid(id);
    }

    @Override
    public boolean findOrtherAdvertise(Advertise advertise)
    {
        List<Advertise> advertises = null;
        Integer id = advertise.getId();
        String code = advertise.getCode();
        if (id != null && !"".equals(id))
        {
            Example exp = new Example(Advertise.class);
            exp.createCriteria().andCondition(" code = '"+ code + "' and id <> "+id);
            advertises = mapper.selectByExample(exp);
        }
        else
        {
            Example exp = new Example(Advertise.class);
            exp.createCriteria().andCondition(" code = '"+ code + "'");
            advertises = mapper.selectByExample(exp);
        }
        if (advertises.size() > 0)
        {
            return true;
        }
        
        return false;
    }

	@Override
	public Pagination getAdvertiseImgPage(int pageno, int pagesize, Map<String, String> searchMap) {
	      Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
			List<AdvertiseImg> List = imgMapper.getAdvertiseImgByParams(searchMap);
			Pagination p = new Pagination(startPage, List);
			p.setList(List);
			p.setParams(searchMap);
	        return p;
	}

	@Override
	public void deleteImgById(Integer id) {
		imgMapper.deleteByPrimaryKey(id);
		
	}

	@Override
	public void saveAdvertiseImg(AdvertiseImg advertiseimg) {
        if(advertiseimg != null) {
            if (advertiseimg.getId() == null) {
                imgMapper.insertSelective(advertiseimg);
            } else {
                imgMapper.updateByPrimaryKeySelective(advertiseimg);
            }
        }
	}

	@Override
	public void editAdvertiseImg(AdvertiseImg advertiseimg) {
		imgMapper.updateByPrimaryKeySelective(advertiseimg);
		
	}

	@Override
	public List<Advertise> getAdvertiseList() {
		return mapper.selectAll();
	}
}
